﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBN
{
	public partial class FrmSearch : Form
	{
		public FrmMain FrmMain { get; set; }
		public FrmSearch()
		{
			InitializeComponent();
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			search();
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			// Ignore clicks that are not on button cells.  
			if (e.RowIndex < 0 || e.ColumnIndex != 0) return;
			
			// Retrieve the ID.
			Guid id = (Guid)dataGridView1[1, e.RowIndex].Value;

			using (DBEntities context = new DBEntities())
			{				
				FrmMain.SetData(context.BenhNhans.FirstOrDefault(b => b.ID == id));
				Close();
			}
		}

        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == (char) 13)
            {
                search();
            }
        }

        private void search()
        {
            using (DBEntities context = new DBEntities())
            {
                bool isnull = string.IsNullOrEmpty(txtName.Text);
                var bns = context.BenhNhans.Where( b => (isnull || b.HoTen.Contains(txtName.Text)) && (nAge.Value == 0 || b.Tuoi == nAge.Value));
                benhNhanBindingSource.DataSource = bns;
            }

        }
	}
}
